package com.coder520.commom.utils;

import sun.misc.BASE64Encoder;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @Author 高鑫【gx_wanmei@163.com】
 * Created by 2017-6-21.17:20
 */
public class SecurityUtils {
    /**
     *  明文密码加密
     * @param password
     * @return
     * @throws NoSuchAlgorithmException
     * @throws UnsupportedEncodingException
     */
    public static String encrptyPassword(String password) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        MessageDigest md5 =MessageDigest.getInstance("MD5");
        BASE64Encoder base64Encoder = new BASE64Encoder();
        String result=base64Encoder.encode(md5.digest(password.getBytes("utf-8")));
        return  result;
    }

    /**
     * 密码校验
     * @param inputPwd
     * @param dbPwd
     * @return
     * @throws UnsupportedEncodingException
     * @throws NoSuchAlgorithmException
     */
    public static boolean checkPassword(String inputPwd,String dbPwd) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        String result= encrptyPassword(inputPwd);
        if (result.equals(dbPwd))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}
